﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using vensoft.Entities;

namespace vensoft.DataAccess
{
    public class DetalleAlmacenDataAcess : MasterDataAcess, IDetalleAlmacenDataAccess 
    {
       
        public DetalleAlmacenDataAcess()
        {
            
        }    

        public List<DetalleAlmacen> GetProductosFromAlmacen(int codigo)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromDetalleAlmacen");
            command.Parameters.AddWithValue("@cod_alm", codigo);
            return SelectObjectFactory(command);
        }

        private List<DetalleAlmacen> SelectObjectFactory(SqlCommand command)
        {
            List<DetalleAlmacen> lista = new List<DetalleAlmacen>();

            using (SqlDataReader reader = base.ExecuteReader(command))
            {
                while (reader.Read())
                {                  
                    DetalleAlmacen producto = new DetalleAlmacen()
                    {
                        Nombre_Prod = reader.GetString(5),
                        Ubic_Alm = reader.GetString(6),
                        Stock_T = reader.GetInt32(4),
                        Stock_B = reader.GetInt32(2),
                        Stock_M = reader.GetInt32(3)                       
                    };

                    lista.Add(producto);
                }
            }

            return lista;
        }

        public void RegistrarIngresoStock(DetalleAlmacen detalle, Producto producto)
        {
            SqlCommand command = base.CreateCommand("RegistrarStockIngreso");
            AddParameterso(detalle,producto, command);
        }

        private void AddParameterso(DetalleAlmacen detalle, Producto producto, SqlCommand command)
        {
            base.AddInParameter(command, "Cod_Producto", producto.Id_Producto);
            base.AddInParameter(command, "Nombre_Prod", producto.Nombre);
            base.AddInParameter(command, "Cod_Almacen", detalle.Cod_Alm);
            base.AddInParameter(command, "Stock_B", detalle.Stock_B);
            base.AddInParameter(command, "Stock_M", detalle.Stock_M);
            base.AddInParameter(command, "Stock_T", detalle.Stock_B + detalle.Stock_M);
            base.AddInParameter(command, "Ubic_Alm", detalle.Ubic_Alm);

            base.ExecuteCommand(command);
        }


        public void ReducirStock(int cod_prod, int cod_alm, int cantidad)
        {
            SqlCommand command = base.CreateCommand("ReducirStockOrigen");
            AddParameterso_Reducir(cod_prod, cod_alm, cantidad, command);
        }

        private void AddParameterso_Reducir(int cod_prod, int cod_alm, int cantidad, SqlCommand command)
        {
            base.AddInParameter(command, "Cantidad", cantidad);
            base.AddInParameter(command, "Id_Producto",cod_prod);
            base.AddInParameter(command, "Id_Almacen",cod_alm);

            base.ExecuteCommand(command);
        }

        public List<DetalleAlmacen> GetProductosFromAlmacenByNombre(string nombre, int cod_alm)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromDetalleAlmacenByNombre");
            command.Parameters.AddWithValue("@nombre_prod", nombre);
            command.Parameters.AddWithValue("@cod_alm", cod_alm);
            return SelectObjectFactory(command);
        }
    
        public int Retornar_Stock(int cod_alm, int cod_prod)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromDetalleAlmacenStockMalo");
            command.Parameters.AddWithValue("@cod_alm", cod_alm);
            command.Parameters.AddWithValue("@cod_prod", cod_prod);
            return Stock_M(command);
        }

        private int Stock_M(SqlCommand command)
        {
            int Stock_M = new int();            
            using (SqlDataReader reader = base.ExecuteReader(command))
            {
                while (reader.Read())
                {
                    Stock_M = reader.GetInt32(3);                          
                }
            }

            return Stock_M;
        }

        public int Retornar_Stock_B(int cod_alm, int cod_prod)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromDetalleAlmacenStockMalo");
            command.Parameters.AddWithValue("@cod_alm", cod_alm);
            command.Parameters.AddWithValue("@cod_prod", cod_prod);
            return Stock_B(command);
        }

        private int Stock_B(SqlCommand command)
        {
            int Stock_B = new int();
            using (SqlDataReader reader = base.ExecuteReader(command))
            {
                while (reader.Read())
                {
                    Stock_B = reader.GetInt32(2);
                }
            }

            return Stock_B;
        }

        public void ActualizarStockDetalleAlmacen(int mov, int cant, int cod_alm, int cod_prod)
        {
            SqlCommand command = base.CreateCommand("ActualizarStockDetalleAlmacen");
            AddParameterso_Stock(mov, cant, cod_alm, cod_prod, command);
        }

        private void AddParameterso_Stock(int mov, int cant, int cod_alm, int cod_prod, SqlCommand command)
        {
            base.AddInParameter(command, "movimiento", mov);
            base.AddInParameter(command, "cantidad", cant);
            base.AddInParameter(command, "cod_prod", cod_prod);
            base.AddInParameter(command, "cod_alm", cod_alm);            

            base.ExecuteCommand(command);
        }

        public void RegistrarDetallePedido(List<DetallePedido> productos, int cod_alm, string ubic_alm)
        {
            foreach (var producto in productos)
            {
                RegistrarDetalle(producto,cod_alm,ubic_alm);
            }
        }

        private void RegistrarDetalle(DetallePedido producto, int cod_alm, string ubic_alm)
        {
            SqlCommand comand = base.CreateCommand("RegistrarDetallePedido");
            base.AddInParameter(comand, "Cod_Prod", producto.ProductoId);
            base.AddInParameter(comand, "Cod_Almacen", cod_alm);
            base.AddInParameter(comand, "Cantidad", producto.Cantidad);
            base.AddInParameter(comand, "nomb_prod", producto.ProductoName);
            base.AddInParameter(comand, "Ubic_alm", ubic_alm);

            base.ExecuteCommand(comand);
        }

        public void RegistrarNuevoProductoEnAlmacen(Producto producto, Almacen almacen, Int32 id_prod)
        {
            SqlCommand comand = base.CreateCommand("RegistrarNuevoProductoEnAlmacen");
            base.AddInParameter(comand, "Cod_Producto", id_prod);
            base.AddInParameter(comand, "Cod_Almacen", almacen.ID_Almacen);
            base.AddInParameter(comand, "Cantidad", producto.Stock);
            base.AddInParameter(comand, "Nombre_Prod", producto.Nombre);
            base.AddInParameter(comand, "Ubic_alm", almacen.Ubicacion);

            base.ExecuteCommand(comand);
        }
    }
}
